package com.fzy.modules.people.web;

import com.fzy.core.common.ApiController;
import com.fzy.core.common.ResponseData;
import com.fzy.modules.bill.service.SoBillService;
import com.fzy.modules.people.entity.Customer;
import com.fzy.modules.people.service.CustomerService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
/**
 * 客户Controller
 */
@RestController
@RequestMapping(value = "/api/people/customer")
public class CustomerController extends ApiController{
    @Autowired
    CustomerService customerService;
    @Autowired
    SoBillService soBillService;
    @GetMapping()
    public ResponseEntity<ResponseData> findAllByPage(Customer customer,@RequestParam(value = "page", defaultValue = "1") int page,
                                                @RequestParam(value = "rows", defaultValue = "10") int rows) {
        if(customer.getName() != null){
            try{
                String name=new String(customer.getName().getBytes("ISO8859-1"),"UTF-8");
                customer.setName(name);
            }catch (IOException e){
                return new ResponseEntity<>(fail(e), HttpStatus.BAD_GATEWAY);
            }
        }
        return new ResponseEntity<>(success(customerService.findAllByPage(customer, (page), rows)), HttpStatus.OK);
    }
    /**
     * app端同步联系人控制器
     * @return
     */
    
    @GetMapping("findAll")
    public ResponseEntity<ResponseData> findAll(){
    	return new ResponseEntity<>(success(customerService.findAll()), HttpStatus.OK);
    }
    
    @GetMapping(value = "/getCode")
    public List<Customer> getCode(){
        return customerService.getCode();
    }

    @PostMapping(value = "")
    public ResponseEntity<ResponseData> addCustomer(Customer customer){
    	//System.out.println(customer.toString());
        if(StringUtils.isNotEmpty(customer.getId())){//更新客户信息
            if(customerService.isOne(customer).size()>0){
                return new ResponseEntity<>(fail("该客户编码已经存在！"), HttpStatus.BAD_GATEWAY);
            }
            return new ResponseEntity<>(success(customerService.updateCustomer(customer)), HttpStatus.OK);
        }//添加客户信息
        if(customerService.isOne(customer).size()>0){
            return new ResponseEntity<>(fail("该客户编码已经存在！"), HttpStatus.BAD_GATEWAY);
        }
		try {
			customer = customerService.addCustomer(customer);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseEntity<>(fail("添加失败，请联系管理员！"), HttpStatus.BAD_GATEWAY);
		}
        if(customer!=null){
        	return new ResponseEntity<>(success(customer), HttpStatus.OK);
        }else{
        	return new ResponseEntity<>(fail("添加失败，请联系管理员！"), HttpStatus.BAD_GATEWAY);
        }
        
    }
    @DeleteMapping(value = "/{id}")
    public ResponseEntity<ResponseData> delCustmoer(@PathVariable String id){
        if(soBillService.isWithCustomer(id) > 0){
            return new ResponseEntity<>(fail("该客户存在销售订单记录，不可删除！"), HttpStatus.BAD_GATEWAY);
        }
        return new ResponseEntity<>(success(customerService.delCustomer(id)), HttpStatus.OK);
    }
}
